const rules = require("../utils/rules");
const { create_token } = require("../utils/token");
const UserModel = require("../model/user");
const index = async (ctx, next) => {
  ctx.body = "主人！";
};
// 用户登录
const login = async (ctx, next) => {
  const { userName, password } = ctx.request.body;
  const find = await UserModel.findOne({ $or: [{ userName }] });
  if (find == null) {
    ctx.body = rules.json(1, "该账号不存在");
    return;
  }
  let findUser = await await UserModel.findOne({ $or: [{ userName, password }] });
  if (findUser == null) {
    ctx.body = rules.json(1, "账号或密码错误");
    return;
  }
  // else {
  //     ctx.status = 200
  //     ctx.body = rules.json(1, "账号已经登录");
  //     return
  // }
  let data = {
    nickName: findUser.nickname,
    userName: findUser.userName,
    avatar: findUser.avatar,
    token: create_token(findUser.id),
    id: findUser._id
  };
  const json = rules.json(0, "登陆成功", data);
  ctx.body = json;
};
// 注册用户
const register = async (ctx) => {
  const { userName, password, nickname, avatar, gender, mobilePhone } = ctx.request.body;
  if (!rules.check_user_id(userName.length)) {
    ctx.status = 404;
    ctx.body = rules.json(1, "账号长度应为6-16");
    return;
  } else if (password.length < 6) {
    ctx.status = 404;
    ctx.body = rules.json(1, "密码长度应大于6");
    return;
  }
  // else if (!rules.test(userName) || !rules.test(password) || !rules.test2(userName)) {
  //     ctx.status = 404;
  //     ctx.body = rules.json(1, '账号密码应为英文数字账号不能是纯数字');
  //     return
  // }
  let find = await UserModel.findOne({ $or: [{ userName }] });
  if (find !== null) {
    ctx.status = 200;
    ctx.body = rules.json(1, "该账号已经存在");
    return;
  }
  // 注册账号
  const userEntity = new UserModel({
    userName,
    password,
    mobilePhone,
    nickname,
    gender,
    avatar
  });
  await userEntity.save();
  const obj = {
    userName: userEntity.userName,
    avatar: userEntity.avatar,
    id: userEntity._id,
    type: "me",
    friendId: userEntity._id
  };
  await serverAddConversationList(userName, obj);
  ctx.body = rules.json(0, "注册成功");
};
// 获取登录用户信息
const getUserInfo = async (ctx) => {
  const data = ctx.request.body;
  try {
    const userDoc = await UserModel.findById(data._id);
    if (userDoc === null) {
      return ctx.body = rules.json(-1, "获取个人信息失败");
    }
    ctx.body = rules.json(200, "获取个人信息失败", {
      userName: userDoc.userName,
      mobilePhone: userDoc.mobilePhone,
      avatar: userDoc.avatar,
      signature: userDoc.signature,
      nickname: userDoc.nickname,
      email: userDoc.email,
      province: userDoc.province,
      city: userDoc.city,
      gender: userDoc.gender,
      conversationsList: userDoc.conversationsList,
      age: userDoc.age,
      friendsGroup: userDoc.friendsGroup,
      id: userDoc._id
    });

  } catch (error) {
    console.log(error);
  }
};
// 获取个人以及好友列表信息 分组状态
const previewUser = async (ctx) => {
  const { id } = ctx.request.body;
  // console.log(id);
  try {
    const userDoc = await UserModel.findById(id);
    if (userDoc === null) return (ctx.body = { code: -1, msg: "获取失败" });

    ctx.body = {
      code: 200,
      data: {
        userName: userDoc.userName,
        mobilePhone: userDoc.mobilePhone,
        avatar: userDoc.avatar,
        signature: userDoc.signature,
        nickname: userDoc.nickname,
        email: userDoc.email,
        province: userDoc.province,
        city: userDoc.city,
        gender: userDoc.gender,
        conversationsList: userDoc.conversationsList,
        age: userDoc.age,
        friendsGroup: userDoc.friendsGroup,
        id: userDoc._id
      },
      msg: "查找成功"
    };
  } catch (error) {
    console.log(error);
  }
};
// 搜索用户
const searchFriends = async (ctx) => {
  const { userName } = ctx.request.query;
  const arr = [{ userName: userName }, { mobilePhone: userName }];
  try {
    const userDoc = await UserModel.findOne({ $or: arr });
    if (userDoc === null) return (ctx.body = { code: -1, msg: "该用户不存在" });
    console.log(userDoc);
    ctx.body = rules.json(200, "查找成功", [{
      userName: userDoc.userName,
      nickname: userDoc.nickname,
      avatar: userDoc.avatar,
      id: userDoc.id
    }]);
  } catch (error) {
    console.log(error);
  }
};
const serverAddConversationList = async (userName, obj) => {
  try {
    const result = await UserModel.updateOne(
      {
        userName
      },
      {
        $push: {
          conversationsList: obj
        }
      }
    );
    if (result === null) return { code: -1, msg: "添加失败" };
    return {
      code: 200,
      msg: "添加成功"
    };
  } catch (error) {
    console.log(error);
  }
};
module.exports = { index, login, register, searchFriends ,serverAddConversationList};
